<?php 
function shijian($data){
	$str="";
    $now=  mktime();
    $t =$now - $data;
   
    if($t >= 24*60*60){
    	$str= round($t / (24*60*60))."天前";
    }else{
    	if($t >= 60*60){
    		$str = round($t /(60*60))."小时前";
    	}else{
    		if($t >= 60){
    			$str=round($t/60)."分钟前";
    		}else{
    			$str=round($t)."秒前";
    		}
    		
    	}
    }
    return $str;
}
 define('EARTH_RADIUS', 6378.137);//地球半径，假设地球是规则的球体
    define('PI', 3.1415926);
    /**
     *  计算两组经纬度坐标 之间的距离
     *   params ：lat1 纬度1； lng1 经度1； lat2 纬度2； lng2 经度2； len_type （1:米 or 2:(千米));
     *   return m or km
     */
    function GetDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2)
    {
       $radLat1 = $lat1 * PI ()/ 180.0;   //PI()圆周率
       $radLat2 = $lat2 * PI() / 180.0;
       $a = $radLat1 - $radLat2;
       $b = ($lng1 * PI() / 180.0) - ($lng2 * PI() / 180.0);
       $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
       $s = $s * EARTH_RADIUS;
       $s = round($s * 1000);
      /* if ($len_type > 1)
       {
           $s /= 1000;
       }*/
       if($s >1000){
        $s = $s /1000;
        return round($s, $decimal)."公里";
       }else{
       	
       	return round($s, $decimal)."米";
       }
     
   
}

  function cutStr($str,$lend){
     $str = strip_tags($str);
	if(mb_strlen($str,"utf-8")>$lend){
		$str = mb_substr($str, 0,$lend,"utf-8")."...";
	}
	return $str;
}

function getOrderNumber(){
	
   return 	date('Ymdhis').str_pad(mt_rand(1, 99999),5,'0',STR_PAD_LEFT); 
}



/**
 * 
 * 判断用户是否登录 如果是范围用户ID 否则返回0
 */
  function checkUserCookie(){
  	
  	 if(empty($_COOKIE['uid'])||empty($_COOKIE['username'])||empty($_COOKIE['key']) ){
  	 	
  	 	  return 0;
  	 }else{
  	 	        
  	 	 $uinfo = M('User')->where("uid=".cookie('uid'))->find();
  	 	 $key = md5($uinfo['username'].$uinfo['password'].C('USER_KEY'));
  	 	 if($key == cookie('key')){
  	 	 	 return $uinfo;
  	 	 }else{
  	 	 	return 0;
  	 	 }
  	 }
  }
  
  /**
   * 
   * 写入用户COOKIE
   * @param $username 用户名
   * @param $password  MD5加密后的密码
   */
  function writeUserCookie($uid,$username,$password){
  	 
  	 $key = md5($username.$password.C('USER_KEY'));
  	 cookie('username',$username);
  	 cookie('uid',$uid);
  	 cookie('key',$key);
  	
  }
  
  function logout(){
  	
  	cookie('username',null);
  	cookie('uid',null);
  	cookie('key',null);
  }
  
  /**
   * 检查用户能发帖数量
   * Enter description here ...
   * @param $type
   */
  function checkUserRating($type){
  	 switch($type){
  	 	case 0:
  	 		return 10;
  	 		break;
  	 	case 1:
  	 		return 10;
  	 		break;	
  	 	case 2:
  	 		return 100;
  	 		break;	
  	 	case 3:
  	 		return 150;
  	 		break;
  	 	case 4:
  	 		return 180;
  	 		break;		
  	 	
  	 }
  	  
  }

function isMobile() {
    // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
        return true;
    }
    // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    if (isset($_SERVER['HTTP_VIA'])) {
        // 找不到为flase,否则为true
        return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
    }
    // 脑残法，判断手机发送的客户端标志,兼容性有待提高。其中'MicroMessenger'是电脑微信
    if (isset($_SERVER['HTTP_USER_AGENT'])) {
        $clientkeywords = array('nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile','MicroMessenger');
        // 从HTTP_USER_AGENT中查找手机浏览器的关键字
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
            return true;
        }
    }
    // 协议法，因为有可能不准确，放到最后判断
    if (isset ($_SERVER['HTTP_ACCEPT'])) {
        // 如果只支持wml并且不支持html那一定是移动设备
        // 如果支持wml和html但是wml在html之前则是移动设备
        if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
            return true;
        }
    }
    return false;
}

function checkStr($str){
      $key = M('Config')->where("id=1")->getField("pingbi");
    $arrKey = explode(",",$key);
    $info = str_replace($arrKey,"MBL",$str);
    $count = substr_count($info,"MBL");
    if($count >0){
        return true;
    }else{
        return false;
    }
}

 ?>
